"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.frameworkService = exports.FrameworkService = void 0;
const log_1 = require("ee-core/log");
const jobs_1 = require("ee-core/jobs");
// framework service
class FrameworkService {
    constructor() {
        Object.defineProperty(this, "myTimer", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "myJob", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "myJobPool", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "taskForJob", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        // 在构造函数中初始化一些变量
        this.myTimer = 0;
        this.myJob = new jobs_1.ChildJob();
        this.myJobPool = new jobs_1.ChildPoolJob();
        this.taskForJob = {};
    }
    /**
     * test
     */
    test(args) {
        return __awaiter(this, void 0, void 0, function* () {
            let obj = {
                status: 'ok',
                params: args
            };
            log_1.logger.info('FrameworkService obj:', obj);
            return obj;
        });
    }
    /**
     * ipc通信(双向)
     */
    bothWayMessage(type, content, event) {
        // 前端ipc频道 channel
        const channel = 'controller/framework/ipcSendMsg';
        if (type == 'start') {
            // 每隔1秒，向前端页面发送消息
            // 用定时器模拟
            this.myTimer = setInterval((e, c, msg) => {
                let timeNow = Date.now();
                let data = msg + ':' + timeNow;
                e.reply(`${c}`, data);
            }, 1000, event, channel, content);
            return '开始了';
        }
        else if (type == 'end') {
            clearInterval(this.myTimer);
            return '停止了';
        }
        else {
            return 'ohther';
        }
    }
    /**
     * 执行任务
     */
    doJob(jobId, action, event) {
        const res = {
            pid: 0,
        };
        let oneTask;
        const channel = 'controller/framework/timerJobProgress';
        if (action == 'create') {
            // 执行任务及监听进度
            let eventName = 'job-timer-progress-' + jobId;
            const timerTask = this.myJob.exec('./jobs/example/timer', { jobId });
            timerTask.emitter.on(eventName, (data) => {
                log_1.logger.info('[main-process] timerTask, from TimerJob data:', data);
                // 发送数据到渲染进程
                event.sender.send(`${channel}`, data);
            });
            // 执行任务及监听进度 异步
            // myjob.execPromise('./jobs/example/timer', {jobId}).then(task => {
            //   task.emitter.on(eventName, (data) => {
            //     Log.info('[main-process] timerTask, from TimerJob data:', data);
            //     // 发送数据到渲染进程
            //     event.sender.send(`${channel}`, data)
            //   })
            // });
            res.pid = timerTask.pid;
            this.taskForJob[jobId] = timerTask;
        }
        if (action == 'close') {
            oneTask = this.taskForJob[jobId];
            oneTask.kill();
            event.sender.send(`${channel}`, { jobId, number: 0, pid: 0 });
        }
        if (action == 'pause') {
            oneTask = this.taskForJob[jobId];
            oneTask.callFunc('./jobs/example/timer', 'pause', jobId);
        }
        if (action == 'resume') {
            oneTask = this.taskForJob[jobId];
            oneTask.callFunc('./jobs/example/timer', 'resume', jobId, oneTask.pid);
        }
        return res;
    }
    /**
     * 创建pool
     */
    doCreatePool(num, event) {
        const channel = 'controller/framework/createPoolNotice';
        this.myJobPool.create(num).then((pids) => {
            event.reply(`${channel}`, pids);
        });
    }
    /**
     * 通过进程池执行任务
     */
    doJobByPool(jobId, action, event) {
        let res = {
            pid: 0,
        };
        const channel = 'controller/framework/timerJobProgress';
        if (action == 'run') {
            // 异步-执行任务及监听进度
            this.myJobPool.runPromise('./jobs/example/timer', { jobId }).then((task) => {
                // 监听器名称唯一，否则会出现重复监听。
                // 任务完成时，需要移除监听器，防止内存泄漏
                let eventName = 'job-timer-progress-' + jobId;
                task.emitter.on(eventName, (data) => {
                    log_1.logger.info('[main-process] [ChildPoolJob] timerTask, from TimerJob data:', data);
                    // 发送数据到渲染进程
                    event.sender.send(`${channel}`, data);
                    // 如果收到任务完成的消息，移除监听器
                    if (data.end) {
                        task.emitter.removeAllListeners(eventName);
                    }
                });
                res.pid = task.pid;
            });
        }
        return res;
    }
    /**
     * 获取正在运行的 job 进程
     */
    monitorJob() {
        setInterval(() => {
            let jobPids = this.myJob.getPids();
            let jobPoolPids = this.myJobPool.getPids();
            log_1.logger.info(`[main-process] [monitorJob] jobPids: ${jobPids}, jobPoolPids: ${jobPoolPids}`);
        }, 5000);
    }
}
exports.FrameworkService = FrameworkService;
FrameworkService.toString = () => '[class FrameworkService]';
const frameworkService = new FrameworkService();
exports.frameworkService = frameworkService;
